import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:whale/helper/component/base/anchor/view.dart';
import 'package:whale/helper/utils/util.dart';

import 'index.dart';
import 'widgets/widgets.dart';

class AnchorDemoPage extends GetView<AnchorDemoController> {
  const AnchorDemoPage({Key? key}) : super(key: key);

  Color _itemColor(int index) {
    switch (index) {
      case 0:
        return Colors.amber;
      case 1:
        return Colors.brown;
      case 2:
        return Colors.purple;
      case 3:
        return Colors.blueAccent;

      default:
        return Colors.deepOrange;
    }
  }

  // 主视图
  Widget _buildView() {
    final state = controller.state;
    return AnchorView(
      tabTitles: state.titles,
      bindItemsBuilder: (context, index) {
        final t = state.titles[index];
        return Container(
          margin: const EdgeInsets.only(top: 20),
          color: _itemColor(index),
          height: 100.0 * (index + 1),
          child: Text('$index-----$t'),
        );
      },
      tabBuilder: (curr, controller) {
        return TabBar(
            controller: controller,
            tabs: state.titles
                .map((e) => Tab(
                      text: e,
                    ))
                .toList());
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AnchorDemoController>(
      builder: (_) {
        return Scaffold(
          appBar: AppBar(title: const Text("anchor_demo")),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
